package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.common.anno.Logs;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessPageDTO;
import com.qk.dto.business.BusinessPoolPageDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 *
 *
 * @author blusr
 * @version 1.0
 * @date 2025/10/11 20:10
 */
@Slf4j
@RestController
@RequestMapping("/businesses")
@SuppressWarnings("all")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @GetMapping("/pool")
    public Result getPool(BusinessPoolPageDTO dto) {
        log.info("查询公海业务列表，参数：{}", dto);
        return Result.success(businessService.getPoolPage(dto));
    }

    @Logs("将业务转换为客户")
    @PostMapping("toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id) {
        log.info("将业务转换为客户，参数：{}", id);
        businessService.toCustomer(id);
        return Result.success();
    }

    @Logs("将业务踢回公海")
    @PutMapping("/back/{id}")
    public Result backToPool(@PathVariable Integer id) {
        log.info("将业务踢回公海，参数：{}", id);
        businessService.updateById(Business.builder().id(id).updateTime(LocalDateTime.now()).status(BusinessStatusConstants.BUSINESS_STATUS_RECYCLED).build());
        return Result.success();
    }

    @GetMapping
    public Result page(BusinessPageDTO businessDTO) {
        log.info("分页查询业务列表，参数：{}", businessDTO);
        return Result.success(businessService.page(businessDTO));
    }

    @Logs("分配业务给用户")
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assignBusiness(@PathVariable Integer businessId, @PathVariable Integer userId) {
        log.info("分配业务给用户，参数：{}，{}", businessId, userId);
        businessService.updateById(Business.builder().id(businessId).userId(userId).updateTime(LocalDateTime.now()).status(BusinessStatusConstants.BUSINESS_STATUS_FOLLOWED).build());
        return Result.success();
    }

    @Logs("新增业务")
    @PostMapping
    public Result save(@RequestBody BusinessDTO dto) {
        log.info("新增业务，参数：{}", dto);
        businessService.addBusiness(dto);
        return Result.success();
    }

    @Logs("根据ID查询业务")
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id) {
        log.info("根据ID查询业务，参数：{}", id);
        return Result.success(businessService.getBusinessById(id));
    }

    @Logs("更新业务")
    @PutMapping
    public Result updateBusiness(@RequestBody UpdateBusinessInfoDTO dto) {
        log.info("更新业务，参数：{}", dto);
        businessService.updateBusinessInfo(dto);
        return Result.success();
    }

}
